package com.song.ejf.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.song.ejf.dao.BaseDAO;
import com.song.ejf.page.Page;
import com.song.ejf.pojo.SResourceRole;
import com.song.ejf.pojo.SRole;
import com.song.ejf.service.RoleService;
import com.song.ejf.util.DateUtil;
import com.song.ejf.util.UUIDUtil;
@Service("roleService")
public class RoleServiceImpl implements RoleService {
	@Autowired
	BaseDAO baseDAO;
	@Override
	public void loadRolePage(Page page, String roleName)  throws Exception{
		if(roleName == null){
			roleName = "";
		}
		Map<String, String> paraMap = new HashMap<String, String>();
		paraMap.put("roleName", "%" + roleName.trim() + "%");
		String selectSql = "select r_id,role_name,status,last_modify_time ";
		String mainSql = " select w.r_id,w.role_name,w.status,w.last_modify_time,rownum RN " +
				"from s_role w where 1=1 and w.role_name like :roleName ";
		String order = " order by w.role_name";
//		String pageSql =  " where RN > " + page.getStartIndex() + " and RN < " + page.getEndIndex() + " ";
		baseDAO.sqlPageQuery(page, selectSql, mainSql, order, paraMap);

	}

	@Override
	public boolean delRole(String[] checkItems)  throws Exception{
		if(checkItems.length <= 0){
			return false;
		}
		for (int i = 0; i < checkItems.length; i++) {
			baseDAO.delById(SRole.class, checkItems[i]);
		}
		return true;
	}

	@Override
	public void saveOrUpdateRole(SRole role) throws Exception{
		baseDAO.saveOrUpdate(role);

	}

	@Override
	public SRole findRoleById(String id)  throws Exception{
		return (SRole)baseDAO.loadById(SRole.class, id);
	}

	@Override
	public List<Map<String, String>> loadResByRoleId(String roleId)  throws Exception{
		if(StringUtils.isBlank(roleId)){
			return null;
		}
		String mainSql = " select sr.r_id From s_resource_role srr,s_resource sr where srr.resource_id = sr.r_id and srr.role_id = :r_id";
		Map<String, String> paraMap = new HashMap<String, String>();
		paraMap.put("r_id", roleId);
		return baseDAO.sqlQuery(mainSql, paraMap);
	}

	@Override
	public boolean addResRole(String[] checkItems, String roleId)  throws Exception{
		if(StringUtils.isBlank(roleId)){
			return false;
		}
		//删除所的旧的角色资源.
		String delSql = "delete s_resource_role srr where srr.role_id = :roleId";
		Map<String, String> paraMap = new HashMap<String, String>();
		paraMap.put("roleId", roleId);
		baseDAO.exeQuery(delSql, paraMap);
		if(checkItems.length > 0){
			SResourceRole resRole = null;
			String str = null;
			for (int i = 0; i < checkItems.length; i++) {
				str = checkItems[i];
				if(StringUtils.isNotBlank(str)){
					resRole = new SResourceRole();
					resRole.setId(UUIDUtil.makeUUID());
					resRole.setResourceId(str.trim());
					resRole.setRoleId(roleId);
					resRole.setLastModifyTime(DateUtil.formatDate());
					baseDAO.saveOrUpdate(resRole);
				}
			}
		}
		return true;
	}

	@Override
	public List<Map<String, String>> loadAllRes() throws Exception{
		String mainSql = "select sr.r_id,sr.role_name From s_role sr where 1=1";
		return baseDAO.sqlQuery(mainSql, null);
	}

	@Override
	public List<String> loadResByUserId(String userId) throws Exception{
		if(StringUtils.isBlank(userId)){
			return null;
		}
		String mainSql = "select sr.resource_url from s_user_role sur, s_resource_role" +
				" srr ,s_resource sr where sur.role_id = srr.role_id and srr.resource_id = " +
				"sr.r_id and sur.user_id = :userId";
		Map<String, String> paraMap = new HashMap<String, String>();
		paraMap.put("userId", userId);
		List<String> resultList = new ArrayList<String>();
		List<Map<String, String>> listMap = baseDAO.sqlQuery(mainSql, paraMap);
		if(listMap != null){
			for (Map<String, String> map : listMap) {
				for (String key : map.keySet()) {
					resultList.add(map.get(key));
				}
			}
		}
		return resultList;
	}

}
